package com.joyplus.report.business.controller;

import com.alibaba.fastjson.JSON;
import com.joyplus.report.business.document.DistributionPvUvStatisticsDocument;
import com.joyplus.report.business.document.DistributionPvUvStatisticsLocationDocument;
import com.joyplus.report.business.document.DistributionPvUvStatisticsLocationFullDocument;
import com.joyplus.report.business.domain.MdCampaigns;
import com.joyplus.report.business.qo.MdCampaignsQO;
import com.joyplus.report.business.service.MdCampaignsService;
import com.joyplus.report.common.utils.DateUitls;
import com.joyplus.report.core.Result;
import com.joyplus.report.core.ResultGenerator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.joda.time.LocalDate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

/**
 * Controller
 *
 * @author jeff
 * @date 2018-05-29
 */
@RestController
@Api(tags = "")
@RequestMapping("/md/campaigns")
public class MdCampaignsController {

  @Resource
  private MdCampaignsService mdCampaignsService;


  @GetMapping("/detail")
  @ApiOperation(value = "查询详情", response = MdCampaigns.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = false, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result detail(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {


    List planDistribution = mdCampaignsService.getPlanDistribution(id.split(","), startDate, endDate);
    List full = mdCampaignsService.getDistributionFull(id.split(","));
    HashMap<String, Object> map = new HashMap<>();
    List<String> dates = DateUitls.findDates(startDate, endDate);

    map.put("planDistribution", JSON.toJSON(planDistribution));
    map.put("full",full);
    map.put("dates", dates);
    return ResultGenerator.genSuccessResult(map);
  }

  @GetMapping("/hourDetail")
  @ApiOperation(value = "查询详情", response = MdCampaigns.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = false, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result hourDetail(@RequestParam String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    if ("".equals(startDate) || "".equals(endDate)) {
      startDate = endDate = LocalDate.now().toString();
    }
    List data = mdCampaignsService.getPlanDistributionByHour(id.split(","), startDate, endDate);
    return ResultGenerator.genSuccessResult(data);
  }

  @GetMapping("/getPlanDistributionByFre")
  @ApiOperation(value = "查询详情", response = MdCampaigns.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = false, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "pageSize", value = "每页显示数量", paramType = "query", dataType = "int"),
      @ApiImplicitParam(required = false, name = "pageNum", value = "第几页", paramType = "query", dataType = "int")
  })
  public Result getPlanDistributionByFre(
      @RequestParam(required = false, name = "id", defaultValue = "") String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate,
      @RequestParam(required = false, name = "pageSize", defaultValue = "0") int pageSize,
      @RequestParam(required = false, name = "pageNum", defaultValue = "10") int pageNum) {

    if ("".equals(startDate) || "".equals(endDate)) {
      startDate = endDate = LocalDate.now().toString();
    }
    String[] str = new String[]{};
    if (!"".equals(id)) {
      str = id.split(",");
    }

    List data = mdCampaignsService
        .getPlanDistributionByFre(str, startDate, endDate, pageNum, pageSize);
    return ResultGenerator.genSuccessResult(data);
  }

  @GetMapping("/geo")
  @ApiOperation(value = "查询详情", response = MdCampaigns.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = false, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
  })
  public Result geo(@RequestParam(required = false, name = "id", defaultValue = "") String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate) {

    if ("".equals(startDate) || "".equals(endDate)) {
      startDate = endDate = LocalDate.now().toString();
    }
    MdCampaigns mdCampaigns = new MdCampaigns();

    List<DistributionPvUvStatisticsLocationDocument> data = new ArrayList();
    if (!"".equals(id)) {
      data = mdCampaignsService.getPlanDistributionByLocation(id.split(","), startDate, endDate);
    }

    List<DistributionPvUvStatisticsLocationFullDocument> locationFull = mdCampaignsService.getDistributionLocationFull(id.split(","));
    List full = mdCampaignsService.getDistributionFull(id.split(","));
    HashMap<String, Object> map = new HashMap<>();
    map.put("mdCampaigns", mdCampaigns);
    map.put("mapData", data);
    map.put("locationFull",locationFull);
    map.put("full",full);
    return ResultGenerator.genSuccessResult(map);
  }
  @GetMapping("/getPlanDistributionCityByLocation")
  @ApiOperation(value = "查询详情", response = MdCampaigns.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = false, name = "id", value = "主键ID", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "endDate", value = "结束时间", paramType = "query", dataType = "String"),
      @ApiImplicitParam(required = false, name = "province", value = "省份", paramType = "query", dataType = "String")
  })
  public Result getPlanDistributionCityByLocation(@RequestParam(required = false, name = "id", defaultValue = "") String id,
      @RequestParam(required = false, name = "startDate", defaultValue = "") String startDate,
      @RequestParam(required = false, name = "endDate", defaultValue = "") String endDate,@RequestParam(required = false,name = "province",defaultValue = "")String province){
    List planDistributionCityByLocation = mdCampaignsService
        .getPlanDistributionCityByLocation(id.split(","), startDate, endDate, province);
    return ResultGenerator.genSuccessResult(planDistributionCityByLocation);
  }


  @GetMapping("/listPage")
  @ApiOperation(value = "分页查询", response = MdCampaigns.class, notes = "分页查询")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "pageSize", value = "每页显示数量", paramType = "query", dataType = "int"),
      @ApiImplicitParam(required = true, name = "pageNum", value = "第几页", paramType = "query", dataType = "int"),
      @ApiImplicitParam(required = false, name = "campaignName", value = "搜索key", paramType = "query", dataType = "String")
  })
  public Result listPage(MdCampaignsQO mdCampaignsQO) {
    PageInfo<MdCampaigns> p = mdCampaignsService.findPage(mdCampaignsQO);
    return ResultGenerator.genSuccessResult(p);
  }


  @GetMapping("/list")
  public Result list() {
    List<MdCampaigns> all = mdCampaignsService.findAll();
    return ResultGenerator.genSuccessResult(all);
  }


  @GetMapping("/detailReport")
  @ApiOperation(value = "查询详情", response = MdCampaigns.class, notes = "查询详情")
  @ApiImplicitParams({
      @ApiImplicitParam(required = true, name = "id", value = "主键ID", paramType = "query", dataType = "long")
  })
  public Result detailReport(@RequestParam Integer id) {
    List<DistributionPvUvStatisticsDocument> documents = mdCampaignsService
        .AggregationById(id);
    return ResultGenerator.genSuccessResult(documents);
  }


}
